package com.ckm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckm.entity.College;
import com.ckm.exception.BusinessException;
import com.ckm.mapper.CollegeMapper;
import com.ckm.service.CollegeService;
import com.ckm.util.FindObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CollegeServiceImpl implements CollegeService {

    @Autowired
    private CollegeMapper collegeMapper;

    @Override
    public void addCollege(List<College> collegeList) {
        if (collegeList == null || collegeList.isEmpty()) {
            throw new BusinessException(250,"添加数据集为空");
        }
        for (College college : collegeList) {
            if (!findCollege(college).isEmpty()) {
                throw new BusinessException(250,"数据重复:"+college);
            }
            collegeMapper.insert(college);
        }
        log.info("添加数据：{}", collegeList);
    }

    @Override
    public void deleteCollege(Integer id) {
        if (id==null){
            throw new BusinessException(250,"删除参数为空");
        }
        if (collegeMapper.selectById(id)==null){
            throw new BusinessException(250,"无此学院");
        }
        collegeMapper.deleteById(id);
    }

    @Override
    public void updateCollege(College college) {
        if (college.getId()==null){
            throw new BusinessException(250,"更新信息缺少id");
        }
        College select = collegeMapper.selectById(college.getId());
        if (select==null){
            throw new BusinessException(250,"无此学院");
        }
        if (select.equals(college)){
            throw new BusinessException(250,"信息无更改");
        }
        collegeMapper.updateById(college);
    }

    @Override
    public PageInfo<College> getCollegePage(College college, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(findCollege(college));
    }

    @Override
    public List<College> findCollege(College college) {
        QueryWrapper<College> wrapper = new QueryWrapper<>();
        FindObjectUtil.fullQueryWrapper(college,wrapper);
        return collegeMapper.selectList(wrapper);
    }
}
